package com.sptpc.learnspring.controller;

import com.sptpc.learnspring.controller.request.NewOrderRequest;
import com.sptpc.learnspring.global.response.GlobalResponse;
import com.sptpc.learnspring.model.Order;
import com.sptpc.learnspring.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping("/add")
    public GlobalResponse createNewOrder(@Valid @RequestBody NewOrderRequest orderRequest) {
        Order order = orderService.addNewOrder(orderRequest);
        return GlobalResponse.success(order);
    }

    @GetMapping("/{id}")
    public GlobalResponse getOrderWithDrinksById(@PathVariable("id")Long id) {
        return GlobalResponse.success(
                orderService.getOrderWithDrinksById(id)
        );
    }

    // 删除订单 - 只有管理员才能执行
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/del/{id}")
    public GlobalResponse deleteOrder(@PathVariable("id")Long id) {
        orderService.deleteOrderById(id);
        return GlobalResponse.success();
    }
}
